$(function(){
    var buyList=$('#buyList');
    addressClick();
    createHtml();
    totalPrice();
    numChange();
    removeHtml();
    /*地址选择与删除*/
    function addressClick(){
        var addressUl=$('#address ul');
        var addressDiv=$('#address ul li div:first-child');
        addressDiv.click(function(){
            $(this).parents('li').addClass('on').siblings().removeClass('on');
            $(this).find('input').prop("checked",true);
        });
        addressUl.on("click",".remove",function(){
            $(this).parents('li').remove();
        })
    }
    /*写入HTML*/
    function createHtml(){
        var buyDatas=LsyStorage.getArr('buyList');
        var goodsNum=buyDatas.length;
        var str='';
        for(var i=0;i<buyDatas.length;i++){
            str+=
                '<li class="details" data-id="'+buyDatas[i].id+'">'
                +'<a href="pro_details.html?id='+buyDatas[i].id+'"><img src="'+buyDatas[i].imgSrc+'" alt="confirm_order_8_.png"></a>'
                +'<div class="goods">'
                +'<div class="top">'
                +'<h3><a href="pro_details.html?id='+buyDatas[i].id+'">'+buyDatas[i].title+'</a></h3>'
                +'<span>¥<s>'+buyDatas[i].price+'</s></span>'
                +'<div id="num" class="num">'
                +'<img src="img/confirm_order_9.png" alt="confirm_order_9.png" class="reduce">'
                +'<input class="input" type="text" value="'+buyDatas[i].num+'">'
                +'<img src="img/confirm_order_10.png" alt="confirm_order_10.png" class="plus">'
                +'</div>'
                +'<b>收藏</b>'
                +'</div>'
                +'<div class="bottom">'
                +'<span>编号：666<s>'+buyDatas[i].id+'</s></span>'
                +'<b>本订单已免运费</b>'
                +'<img src="img/order_5.png" alt="order_5.png" class="remove">'
                +'</div>'
                +'</div>'
                +'</li>'
        }
        buyList.html(str);
        $('#goodsNum').text(goodsNum);
    }
    /*实付款*/
    function totalPrice(){
        var freightNum=$('#freight').text();
        var freight=$('#freight');
        var buyListLi=$('#buyList li');
        var totalPrice=0;
        var goodsPrice=$('#goodsPrice');
        buyListLi.each(function(key,item){
            var singlePrice=$(item).find('.top span s').text();
            var singleNum=$(item).find('.top div input').val();
            totalPrice+=singlePrice*singleNum;
        });
        if(totalPrice>=100){
            freightNum=0;
        }else{
            if(totalPrice>0){
                freightNum=30;
            }else{
                freightNum=0;
            }
        }
        freight.text(freightNum);
        totalPrice+=parseFloat(freightNum);
        totalPrice=totalPrice.toFixed(2);
        goodsPrice.text(totalPrice);
    }
    /*点击数量改变*/
    function numChange(){
        buyList.on('click','.reduce',function(){
            var input=$(this).parents('.num').find('input');
            var inputNum=$(this).parents('.num').find('input').val();
            inputNum--;
            if(inputNum==0){
                inputNum=1;
            }
            input.val(inputNum);
            totalPrice();
        });
        buyList.on('click','.plus',function(){
            var input=$(this).parents('.num').find('input');
            var inputNum=$(this).parents('.num').find('input').val();
            inputNum++;
            input.val(inputNum);
            totalPrice();
        });
        buyList.on('keyup','.num input',function(){
            var textInp=$(this).val();
            textInp=integer(textInp);
            $(this).val(textInp);
            totalPrice();
        });
        buyList.on('blur','.num input',function(){
            var textInp=$(this).val();
            if(!textInp){
                textInp=1;
            }
            $(this).val(textInp);
            totalPrice();
        });
    }
    /*点击删除  点击提交*/
    function removeHtml(){
        var submit=$('#submit');
        buyList.on('click','.remove',function(){
            var id=$(this).parents('li').attr("data-id");
            $(this).parents('li').remove();
            LsyStorage.removeItem('buyList_'+id);
            totalPrice();
        });
        submit.click(function(){
            LsySession.removeItem('daletes');
            var moneyDatas=$('#goodsPrice').text();
            var buyListLi=buyList.find('li');
            var deleteDatas=[];
            buyListLi.each(function(key,item){
                var id=$(item).attr("data-id");
                deleteDatas.push(id);
            });
            LsySession.setItem('money',moneyDatas);
            LsySession.setItem('deletes',deleteDatas);
            orderStorage();
            window.location.href='pay.html';
        })
    }
    /*存储购买的商品信息*/
    function orderStorage(){
        var buyListLi=buyList.find('li');
        buyListLi.each(function(key,item){
            var id=$(item).attr("data-id");
            var buyListDatas=LsyStorage.getItem('buyList_'+id);
            var num=$(item).find('.num input').val();
            var time=new Date().toLocaleDateString();
            var singlePrice=buyListDatas.price;
            var price=(singlePrice*num).toFixed(2);
            var freight=$('#freight').text();
            time=time.replace(/\//g,'-');
            var orderNumber='s2'+time.replace(/\-/g,'')+parseInt(1000*Math.random());
            buyListDatas.num=num;
            buyListDatas.time=time;
            buyListDatas.orderNumber=orderNumber;
            buyListDatas.price=price;
            buyListDatas.freight=freight;
            LsyStorage.setItem('orderList_'+id,buyListDatas);
        })
    }
});
